package it.polimi.swKnights.SWIMv2.servlets.administration;

import it.polimi.swKnights.SWIMv2.ejb.administration.BanUserRemote;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class AdministrationController
 */
public class AdministrationController extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		String destination = "/WEB-INF/administration/AdministrationPage.jsp";
		BanUserRemote banBean = this.lookUp(BanUserRemote.class);

		this.setMessages(request);

		HttpSession session = request.getSession();
		String admin = (String) session.getAttribute("userName");
		String password = (String) session.getAttribute("password");

		request.setAttribute("bannedList",
				banBean.getBannedUsers(admin, password));

		request.getRequestDispatcher(destination).forward(request, response);
	}

	private void setMessages(HttpServletRequest request)
	{
		String banned = request.getParameter("banned");
		String newFounder = request.getParameter("newFounder");
		String newAdmin = request.getParameter("newAdmin");
		String adminRemoved = request.getParameter("adminRemoved");
		String sbanned = request.getParameter("sbanned");

		if (banned != null)
		{
			request.setAttribute("message", "The User " + banned
					+ " has been correctly banned");
		}
		else if (newFounder != null)
		{
			request.setAttribute("message",
					"fouder has been correctly changed, now the admin is: "
							+ newFounder);
		}
		else if (newAdmin != null)
		{
			request.setAttribute("message", "the user " + newAdmin
					+ " is now one of the Administrators");
		}
		else if (adminRemoved != null)
		{
			request.setAttribute("message", "the user " + adminRemoved
					+ " has been removed from Administrators");
		}
		else if (sbanned != null)
		{
			request.setAttribute("message", "the user " + sbanned
					+ " is not anymore a banned user");
		}

	}

}
